package com.horoscope.astrology.zodiac.signs.free.utils

import android.os.Bundle
import android.util.ArrayMap
import com.facebook.appevents.AppEventsConstants
import com.facebook.appevents.AppEventsLogger
import com.flurry.android.FlurryAgent
import com.google.firebase.analytics.FirebaseAnalytics
import com.horoscope.astrology.zodiac.signs.free.MmoApplication


object FlurryEventUtil {

    val context = MmoApplication.getInstance()


    // Flurry打点 最大监听500个事件，每个事件最多可有10个的参数，值无限
    // FlurryAgent.logEvent(eventType, bundle, true) -> 定时事件 例:点击进入文章
    // FlurryAgent.endTimedEvent（eventType）; -> 离开文章页
    private fun sendEventWithParameters(eventType: String, bundle: Map<String, String>?) {
        /* if (!BuildConfig.DEBUG) {
             FlurryAgent.logEvent(eventType, bundle)
         }*/
        if (bundle != null) {
            FlurryAgent.logEvent(eventType, bundle)
        } else {
            FlurryAgent.logEvent(eventType)
        }
    }

    // FaceBook打点
    private fun sendEnentWithFaceBook(eventType: String, bundle: Bundle?) {
        val logger = AppEventsLogger.newLogger(context)
        if (bundle != null) {
            logger.logEvent(eventType, bundle)
        } else {
            AppEventsLogger.newLogger(context).logEvent(eventType)
        }
    }

    // FireBase打点
    private fun sendEventWithFireBase(eventType: String, bundle: Bundle) {
        FirebaseAnalytics.getInstance(context).logEvent(eventType, bundle)
    }


    fun putDataToEvent(eventType: String, key: String?, value: String?) {
        if (key == null && value == null) {
            sendEventWithParameters(eventType, null)
            sendEnentWithFaceBook(eventType, null)
            sendEventWithFireBase(eventType, Bundle())
        } else {
            val map = ArrayMap<String, String>()
            map[key] = value
            val bundle = Bundle()
            bundle.putString(key, value)

            sendEventWithParameters(eventType, map)
            sendEnentWithFaceBook(eventType, bundle)
            sendEventWithFireBase(eventType, bundle)
        }
        Logger.error("Flurry", "type=$eventType")
    }

    // 因Facebook有成就解锁事件的相关标准事件，故单独向Facebook打点
    fun logUnlockAchievementEvent(description: String?) {
        val logger = AppEventsLogger.newLogger(context)
        val params = Bundle()
        params.putString(AppEventsConstants.EVENT_PARAM_DESCRIPTION, description)
        logger.logEvent(AppEventsConstants.EVENT_NAME_UNLOCKED_ACHIEVEMENT, params)
    }

}